package com.example.project.controller.project;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.project.core.controller.BaseController;
import com.example.project.core.domain.AjaxResult;
import com.example.project.entity.Issue;
import com.example.project.entity.IssueCategoryStats;
import com.example.project.entity.IssueProcessingStats;
import com.example.project.service.IssueService;
import com.example.project.service.IssueStatsService;

/**
 * 需求统计控制器
 */
@Controller
@RequestMapping("/project/issue/stats")
public class IssueStatsController extends BaseController {
    
    @Autowired
    private IssueStatsService statsService;
    
    @Autowired
    private IssueService issueService;
    
    /**
     * 获取需求统计数据
     */
    @RequiresPermissions("project:issue:view")
    @GetMapping("/{issueId}")
    @ResponseBody
    public AjaxResult getIssueStats(@PathVariable Long issueId) {
        try {
            Issue issue = issueService.selectIssuesById(issueId);
            if (issue == null) {
                return error("需求不存在");
            }
            
            Map<String, Object> stats = statsService.getSimilarIssueStats(issue);
            return success(stats);
        } catch (Exception e) {
            return error("获取统计数据失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取需求类别统计
     */
    @RequiresPermissions("project:issue:view")
    @GetMapping("/category")
    @ResponseBody
    public AjaxResult getCategoryStats(
            @RequestParam(required = false) Long categoryId,
            @RequestParam(required = false) Long trackerId,
            @RequestParam(required = false) Long priorityId,
            @RequestParam(required = false) Long projectId) {
        try {
            IssueCategoryStats stats = statsService.getCategoryStats(categoryId, trackerId, priorityId, projectId);
            return success(stats);
        } catch (Exception e) {
            return error("获取类别统计失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取同类需求历史记录
     */
    @RequiresPermissions("project:issue:view")
    @GetMapping("/history/{issueId}")
    @ResponseBody
    public AjaxResult getSimilarHistory(
            @PathVariable Long issueId,
            @RequestParam(defaultValue = "10") Integer limit) {
        try {
            Issue issue = issueService.selectIssuesById(issueId);
            if (issue == null) {
                return error("需求不存在");
            }
            
            List<IssueProcessingStats> history = statsService.getSimilarIssueHistory(issue, limit);
            return success(history);
        } catch (Exception e) {
            return error("获取历史记录失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取用户处理统计
     */
    @RequiresPermissions("project:issue:view")
    @GetMapping("/user/{userId}")
    @ResponseBody
    public AjaxResult getUserStats(
            @PathVariable Long userId,
            @RequestParam(defaultValue = "10") Integer limit) {
        try {
            List<Map<String, Object>> stats = statsService.getUserProcessingStats(userId, limit);
            return success(stats);
        } catch (Exception e) {
            return error("获取用户统计失败: " + e.getMessage());
        }
    }
    
    /**
     * 重新计算类别统计
     */
    @RequiresPermissions("project:issue:edit")
    @GetMapping("/recalculate/{categoryId}")
    @ResponseBody
    public AjaxResult recalculateCategoryStats(@PathVariable Long categoryId) {
        try {
            boolean result = statsService.recalculateCategoryStats(categoryId);
            if (result) {
                return success("重新计算成功");
            } else {
                return error("重新计算失败");
            }
        } catch (Exception e) {
            return error("重新计算失败: " + e.getMessage());
        }
    }
}